$(document).ready(function(){
  $["importTableBox"] = $("div#importTableBox");
  $("#importTableButton").unbind("click").bind('click',importTableEvent);
  function importTableEvent(e){
    $["importTableBox"].setDstTable();
    $["importTableBox"].dialog();
  }
  $["importTableBox"].setDstTable = function(){
    $["importTableBox"].find("tr#tbName td.Dst font").text($("#mainDataTable").attr("datatablename"));
    var colHeads = $("#mainDataTable").find("thead tr th");
    $["importTableBox"].find("tr#tbAllCols td.Dst ul").empty();
    colHeads.each(function(i,th){
      $["importTableBox"].find("tr#tbAllCols td.Dst ul").append("<li>"+ $(th).attr("fieldname") +"</li>");
    });
  }

  $["importTableBox"].find("tr#tbAllCols td.Dst ul li").live('click', function(event) {
    var li = findUpElement(event.target,"tr#tbAllCols td ul li");
    $(li).appendTo($("tr#tbSelCols td.Dst ul"));
  });
  $["importTableBox"].find("tr#tbSelCols td.Dst ul li").live('click', function(event) {
    var li = findUpElement(event.target,"tr#tbSelCols td ul li");
    $(li).appendTo($("tr#tbAllCols td.Dst ul"));
  });
  
  $["importTableBox"].find("tr#tbAllCols td.Src ul li").live('click', function(event) {
    var li = findUpElement(event.target,"tr#tbAllCols td ul li");
    $(li).appendTo($("tr#tbSelCols td.Src ul"));
  });
  $["importTableBox"].find("tr#tbSelCols td.Src ul li").live('click', function(event) {
    var li = findUpElement(event.target,"tr#tbSelCols td ul li");
    $(li).appendTo($("tr#tbAllCols td.Src ul"));
  });
  
  $["importTableBox"].find("div#importSrcTable select").unbind("change").bind('change',function(event){
    var op = $(findUpElement(event.target,"div#importSrcTable select")).find("option:selected");
    if(op.text() == "xlsfile"){
      $["importTableBox"].find("#uploadBox").css("display","inline");
      var url = "/Table/Table.do";
      var data = {
          method: 'getTableInfo',
          tableName: 'uploadTable'
      };
      $.get(url,data,function(response){
        var response = $("<div>" + response + "</div>");
        var tableName = "uploadTable";
        $["importTableBox"].find("tr#tbName td.Src font").text(tableName);
        var colLiList =  $(response).find("li");
        $(colLiList).appendTo($("tr#tbAllCols td.Src ul").empty());
      });
    }
  });
  $["importTableBox"].find("#button").unbind("click").bind('click',function(event){
    var url = "/Table/Table.do";
    var data = {
        datatableName:$("table#mainDataTable").attr('datatablename'),
        method:'run',
        colAction:'import',
        ATableSource:'xlsfile'
    };
    var SrcSelLi =  $["importTableBox"].find("tr#tbSelCols td.Src ul li");
    var DstSelLi =  $["importTableBox"].find("tr#tbSelCols td.Dst ul li");
    SrcSelLi.each(function(i,e){
      data["SrcColNames" + i] = $(e).text();
    });
    DstSelLi.each(function(i,e){
      data["DstColNames" + i] = $(e).text();
    });
    $.get(url,data);
    event.stopPropagation();     
  });
  
});